        import { Reducer, useEffect, useReducer } from "react";
        
        interface Action {
            type: 'add' | 'minus';
            num: number;
        }
        
        function reducer(state: number, action: Action): number {
            switch (action.type) {
                case 'add':
                    return state + action.num;
                case 'minus': 
                    return state - action.num;
                default:
                    return state;
            }
        }
        
        export function Trap() {
            const [count, dispatch] = useReducer<Reducer<number, Action>, number>(reducer, 0);
        
            useEffect(() => {
                console.log(count);
        
                // 保存定时器ID以便清理
                const timer = setInterval(() => {
                    dispatch({ type: 'add', num: 1 });
                }, 1000);
        
                // 组件卸载时清理定时器，防止内存泄漏
                return () => {
                    clearInterval(timer);
                };
            }, []);
        
            return <div>{count}</div>;
        }